package com.example.a16021.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.os.AsyncTask;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import org.json.JSONObject;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class MainActivity extends AppCompatActivity {
    EditText et_mobile;
    Button button;
    TextView tv_location;
    String mobile;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        et_mobile= (EditText) findViewById(R.id.et_mobile);
        button= (Button) findViewById(R.id.btn_query);
        tv_location= (TextView) findViewById(R.id.tv_location);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mobile=et_mobile.getText().toString();
                MobileAsyncTask mobileAsyncTask=new MobileAsyncTask();
                mobileAsyncTask.execute();
            }
        });
    }

    class  MobileAsyncTask  extends AsyncTask<String ,Integer,String> {
        //网络请求等耗时操作都要写在
        @Override
        protected String doInBackground(String... strings) {
            //构造URL
            String path = "http://apis.juhe.cn/mobile/get?phone="+mobile+"&key=22d599d4e9ccff871f3bf797205c56ac";
            try {
                URL url=new URL(path);
                HttpURLConnection connection= (HttpURLConnection) url.openConnection();
                connection.setRequestMethod("GET");
                connection.setReadTimeout(5000);
                connection.setConnectTimeout(5000);
                //发起连接
                connection.connect();
                int code=connection.getResponseCode();
                if (code==200){
                    InputStream inputStream=connection.getInputStream();
                    byte[] bytes=new byte[1024];
                    int len=0;
                    ByteArrayOutputStream bos=new ByteArrayOutputStream();
                    while ((len=inputStream.read(bytes))!= -1){
                        bos.write(bytes,0,len);
                    }
                    String s=new String(bos.toByteArray());
                    System.out.println(s);
                    JSONObject jo=new JSONObject(s);
                    JSONObject resultJo=jo.optJSONObject("result");
                    String province=resultJo.optString("province");
                    String city=resultJo.optString("city");
                    System.out.println(province+city);
                    return province+city;//返回的字符串就是作为参数传递给onPostExcuse方法
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

            return null;
        }

        @Override
        protected void onPostExecute(String s) {
            tv_location.setText(s);
        }
    }

}